<template>
    <div class="sceen3d" :id="id"></div>
</template>

<script>
export default {
    props: {config:Object, shared: {type: Boolean, default: false}},
    data(){
        return {
            id: "cgsTest_" + new Date().getTime()
        }
    },
    mounted (){
      let _this = this
      let headers = {}
      if(this.shared){
        headers['X-Mode'] = 'share'
      }
      const projPromise = (projId) => {
        return new Promise((resolve, reject) => {
            fetch("/api/Cgs/Proj/Get?ID="+ projId, {headers: headers})
            .then(
                function (res) {
                    return res.json();
                }
            ).then(function(json){
                var code= json['Code'];
                if(code === 301){
                    alert('Not Allowed');
                    reject()
                    return;
                }
                let jsCode = json['Data']['Data']
                // loadJs(filePath)
                resolve(jsCode)
            });
        })
      }
      const getApp = (id) => {
        return new Promise ((resolve, reject) => {
            fetch(`/api/Cgs/Portal/Visual/Detail?VisualType=ThreeD&ID=${id}`, {headers: headers})
            .then(response => {
              return response.json()
            })
            .then(data => {
                resolve(data.data)
            });
        })
    }
    let CGSJsSDKLoader = window.CGSJsSDKLoader
    CGSJsSDKLoader.accessToken = this.$props.config.accessToken
    //"pAVGo-01KngnjmkHdRm8zHMTXK7SNt35CQgQjc5oTwBter6aQPvAQsDCGhLKcL8-M4zrZ-2RDOa.dk2RfMLiPcGR5c4ol99d6ukAqcPoq9mDnIppRLTu3LdhUwtD5ryY"

    // 如果是离线版本，请修改 baseUrl 为当前使用的地址
    CGSJsSDKLoader.baseUrl = this.$props.config.baseUrl
    CGSJsSDKLoader.load(success, fail, '', 'white')
    async function success() {
      let container = _this.id;
      let isSdk = true
      let appData = await getApp(_this.$props.config.appID)
      let {visual: {projectID}} = appData;
      let jsCode = await projPromise(projectID)
      eval(jsCode)
    }

    function fail(e) {
      console.log(e)
    }
  },
  methods:{
    resize(newHPx, newWPx){
      // console.log(newHPx, newWPx)
    }
  }

}
</script>

<style scoped>
.sceen3d{
    width: 100%;
    height: 100%;
}
</style>
